/*
 * Copyright (C) 2008 by Xume.
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.xume.critann.annotations.projection;

import org.xume.critann.ProjectionBuilder;

/**
 * $Id: ProjectionType.java 26 2008-10-16 12:45:35Z johan.siebens $
 * 
 * @author Johan Siebens
 */
public enum ProjectionType {

	AVG(new AvgStrategy()), //
	COUNT(new CountStrategy()), //
	MAX(new MaxStrategy()), //
	MIN(new MinStrategy()), //
	PROPERTY(new PropertyStrategy()), //
	ROW_COUNT(new RowCountStrategy()), //
	SUM(new SumStrategy());

	private Strategy strategy;

	private ProjectionType(Strategy strategy) {
		this.strategy = strategy;
	}

	public <P> P createProjection(ProjectionBuilder<P> builder, String propertyName) {
		return strategy.createProjection(builder, propertyName);
	}

	private static class AvgStrategy implements Strategy {

		public <P> P createProjection(ProjectionBuilder<P> builder, String propertyName) {
			return builder.avg(propertyName);
		}
	}

	private static class CountStrategy implements Strategy {

		public <P> P createProjection(ProjectionBuilder<P> builder, String propertyName) {
			return builder.count(propertyName);
		}
	}

	private static class MaxStrategy implements Strategy {

		public <P> P createProjection(ProjectionBuilder<P> builder, String propertyName) {
			return builder.max(propertyName);
		}
	}

	private static class MinStrategy implements Strategy {

		public <P> P createProjection(ProjectionBuilder<P> builder, String propertyName) {
			return builder.min(propertyName);
		}
	}

	private static class PropertyStrategy implements Strategy {

		public <P> P createProjection(ProjectionBuilder<P> builder, String propertyName) {
			return builder.property(propertyName);
		}
	}

	private static class RowCountStrategy implements Strategy {

		public <P> P createProjection(ProjectionBuilder<P> builder, String propertyName) {
			return builder.rowCount();
		}
	}

	private static interface Strategy {

		<P> P createProjection(ProjectionBuilder<P> builder, String propertyName);
	}

	private static class SumStrategy implements Strategy {

		public <P> P createProjection(ProjectionBuilder<P> builder, String propertyName) {
			return builder.sum(propertyName);
		}
	}
}
